Many operating systems support multitasking. A task is a stand-alone application program or a subprogram that is run as an independent entity. Multitasking is the ability of a computer system to have more than one task in memory at a time and to switch which task the CPU is currently executing to improve the efficiency of the computer system's components. Multitasking operating systems provide mechanisms for protecting shared code and data. Shared code is code that can be executed by more than one task, and shared data is data that can be accessed by more than one task. The mechanisms provided by multitasking operating systems prevent problems associated with code sharing and data sharing. Code sharing and data sharing problems can arise if one task has modified shared code or the data that the shared code manipulates and another task begins or continues execution of the shared code after the shared code or its data has been modified. The task that has begun or continued execution of the shared code after modification of the code or its data can get unexpected results due to the modifications to the code or its data.
One type of multitasking operating system is a non-preemptive, or cooperative, multitasking operating system. A cooperative multitasking operating system requires cooperation between the tasks in order for the computer system to function properly. With such an operating system, once a first task has been assigned control of the CPU, all other tasks are blocked (i.e., prevented from gaining control of the CPU) until the first task relinquishes control of the CPU. Thus, shared code and data are inherently protected in such an operating system. Specifically, since control of the CPU cannot be taken away from a task once it has been assigned, it is impossible for one task to have modified shared code or its data and another task to begin or continue execution of the shared code after the shared code or its data has been modified. The task that has been assigned control of the CPU will not relinquish control of the CPU until the task has completed execution of the shared code.
Another type of multitasking operating system is a preemptive multitasking operating system. Unlike a cooperative system, a preemptive multitasking operating system does not require cooperation between the tasks in order for the computer system to function properly. With such an operating system, the system assigns control of the CPU to a task and then takes back control from the task when a specified time expires or a specified event occurs. Thus, shared code and data are not inherently protected in such an operating system. For example, one task may have modified shared code or its data when the operating system takes back control of the CPU from the task. Subsequently, another task may be assigned control of the CPU and may begin or continue execution of the shared code after the shared code or its data has been modified. As a result, this situation can lead to the code sharing and data sharing problems discussed above.